package com.pro01.springlearning.controller;

import com.pro01.springlearning.pojo.Result;
import com.pro01.springlearning.pojo.User;
import com.pro01.springlearning.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result<Void> register(@RequestBody User user) {
        if (userService.findByUsername(user.getUsername()) != null) {
            return Result.error("用户已存在");
        }
        boolean success = userService.register(user);
        if (success) {
            return Result.success();
        } else {
            return Result.error("注册失败");
        }
    }

}
